<template>
  <l-layout :top="149" :topMove="false" class="erpCase">
    <template #top>
      <div class="form_div_back">
        <div class="panel_name_size_18">{{ $t(formData.case_erp_customergather_f_Name) }}</div>

        <div class="form_information panel_margin_top_25">
          <div>{{ $t('计划回款金额：' +
            formData.case_erp_customergather_f_WaitAmount)
          }}</div>
          <div class="panel_margin_left_50">{{ $t('计划回款日期：' +
            formData.case_erp_customergather_f_ReceivedDate)
          }}</div>
          <div class="panel_margin_left_50">{{ $t('最迟回款日期：' +
            formData.case_erp_customergather_f_FinallyDate)
          }}</div>
          <div class="panel_margin_left_50">{{ $t('合同标题：' +
            formData.case_erp_customergather_f_Title)
          }}</div>
          <div class="panel_margin_left_50">{{ $t('合同负责人：' +
            formData.case_erp_customergather_f_ModifyUserName)
          }}</div>
        </div>
      </div>
    </template>
    <div class="l-auto-window h48" style="padding-left: 16px; padding-right: 16px">
      <collection-form :id="id" ref="tableForm" style="height:100%;"></collection-form>
    </div>

  </l-layout>
</template>
<script>
const api = window.$api.erpcase.customerGather
import CollectionForm from './collectionForm';

export default {
  name: 'RecordForm',
  props: {
    isReadOnly: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    },

  },
  components: {
    CollectionForm,
  },
  watch: {
    // recordVisible(newVal, oldVal) {
    //     if(newVal == false){
    //       this.recordVisible = true
    //     }
    //   },
    //   deep: true,
    //   immediate: true
  },
  data() {
    return {
      moduleCode: 'ERP_CustomerReturn',

      formData: {
        case_erp_customergather_f_Name: "", // 客户名称
        case_erp_customergather_f_WaitAmount: "", // 计划回款金额
        case_erp_customergather_f_ReceivedDate: "", // 计划回款日期
        case_erp_customergather_f_FinallyDate: "", // 最迟回款日期
        case_erp_customergather_f_Title: "", // 合同标题
        case_erp_customergather_f_Principal: "", // 合同负责人

      },
      rules: {

      },


    };
  },
  created() {
    this.loadFormData({ keyValue: this.id })
  },
  computed: {},
  methods: {
    handleCase_erp_customergatherdetailAddRow() {
      let point = {};
      point.f_AmountCollect = "";
      point.f_Date = "";
      point.f_CreateDate = "";
      point.f_CreateUserName = "";
      this.case_erp_customergatherdetail_data.push(point);
    },
    handleCase_erp_customergatherdetailDeleteRow(event) {
      this.case_erp_customergatherdetail_data.splice(event.index, 1);
    },

    // 重置表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.case_erp_customergatherdetail_data = [];
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      if (!this.$refs.case_erp_customergatherdetail_table.validate()) {
        return false;
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.formData, data, "case_erp_customergather");
      this.case_erp_customergatherdetail_data = data.caseErpCustomergatherdetailList;
      this.formData.case_erp_customergather_f_ReceivedDate = this.$formatDate(this.formData.case_erp_customergather_f_ReceivedDate,'yyyy-MM-dd')
      this.formData.case_erp_customergather_f_FinallyDate = this.$formatDate(this.formData.case_erp_customergather_f_FinallyDate,'yyyy-MM-dd')
    },
    // 获取表单数据
    getForm() {
      const formData = {};
      formData.caseErpCustomergatherEntity = this.$getFormData(
        this.formData,
        "case_erp_customergather"
      );
      formData.caseErpCustomergatherdetailList =
        this.case_erp_customergatherdetail_data;
      return formData;
    },

    async loadFormData({ keyValue,init}) {
      // console.log('loadFormData')
      if(init == true){
        // console.log("值",init)
        this.$refs.tableForm.init()
      }
      
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // 编辑
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // 新增
        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>